Teach Yourself PHP with MySQL by Nat McBride

Teach Yourself PHP with MySQL by Nat McBride

Author:Nat McBride [McBride, Nat]
Language: eng
Format: mobi
Publisher: Hodder Headline
Published: 2011-05-12T16:00:00+00:00


7.3 Putting it all together

Before we show the full script for this example, it’s worth a quick look at the structure using some pseudocode:

<?

// if the form has been posted, analyse it:

if ($_POST) {

foreach($_POST as $k => $v) {

$v = trim($v) ;

$$k = $v ;

}

$error = “” ;

// cleaning and validation code here...

// ...then display errors or results:

if ($error != “”) {

echo “$error <P>Please try again.” ;

} else {

echo “<P><B>Forename:</B> $forename<BR/>” ;

// etc...

}

// if the form has not been posted, display it!

} else {

?>

<FORM METHOD=”POST” ACTION=”<? echo $_SERVER[‘PHP_SELF’] ?>”>

<!— HTML to display form —>

<?

// remember to close the if... braces!

}

?>

Now for the full code listing:

<HTML>

<HEAD>

<LINK REL=”stylesheet” TYPE=”text/css” HREF=”plain.css” />

</HEAD>

<BODY>

<?

if ($_POST) {

foreach($_POST as $k => $v) {

$v = trim($v) ;

$$k = $v ;

}

// create empty error variable

$error = “” ;

// check for data in required fields

if (($forename == “”) ||

($surname == “”) ||

($email == “”)) {

$error = “Please fill in all the required fields.<BR/>” ;

}

// validate $age

if (is_numeric($age) == FALSE) {

$error = “Please enter a valid age (numbers only!)<BR/>” ;

}

// validate $email

if ((strpos($email, “@”) === FALSE) ||

(strpos($email, “.”) === FALSE) ||

(strpos($email, “ “) != FALSE) ||

(strpos($email, “@”) > strrpos($email, “.”))) {

$error .= “Please enter a valid email address<BR/>” ;

}

// clean and validate $tel

$notel = array(“+”, “ “, “(“, “)”, “[“, “]”, “-”, “,”, “#”) ;

$tel = str_replace($notel, array(“00”), $tel) ;

if (is_numeric($tel)== FALSE) {

$error .= “Please enter a valid telephone number<BR/>” ;

}

// clean $biog and add <br/>s

$biog = stripslashes($biog) ;

$biog = nl2br($biog) ;

if ($error != “”) {

echo “$error <P>Please hit the back button to try again.” ;

} else {

echo “<P><B>Forename:</B> $forename<BR/>” ;

echo “<B>Surname:</B> $surname<BR/>” ;

echo “<B>Age:</B> $age<BR/>” ;

echo “<B>Sex:</B> $sex<BR/>” ;

echo “<B>Email:</B> $email<BR/>” ;

echo “<B>Telephone:</B> $tel<BR/>” ;

echo “<B>Description:</B> $biog<BR/>” ;

}

} else {

?>

<TABLE BORDER=”0” CELLPADDING=”3” CELLSPACING=”0”>

<FORM METHOD=”POST” ACTION=”<? echo $_SERVER[‘PHP_SELF’] ?>”>

<TR>

<TD>Forename:</TD>

<TD><INPUT TYPE=”text” NAME=”forename” SIZE=”45” MAXLENGTH=”100”/></TD>

</TR><TR>

<TD>Surname:</TD>

<TD><INPUT TYPE=”text” NAME=”surname” SIZE=”45” MAXLENGTH=”100”/></TD>

</TR><TR>

<TD>Age:</TD>

<TD><INPUT TYPE=”text” NAME=”age” SIZE=”5” MAXLENGTH=”5”/></TD>

</TR><TR>

<TD>Sex:</TD>

<TD><INPUT TYPE=”radio” NAME=”sex” VALUE=”F”/>F

&#160;&#160; <INPUT TYPE=”radio” NAME=”sex” VALUE=”M”/>M</TD>

</TR><TR>

<TD>Email:</TD>

<TD><INPUT TYPE=”text” NAME=”email” SIZE=”45” MAXLENGTH=”100”/></TD>

</TR><TR>

<TD>Telephone:</TD>

<TD><INPUT TYPE=”text” NAME=”tel” SIZE=”45” MAXLENGTH=”20”/></TD>

</TR><TR>

<TD COLSPAN=”2”>Describe yourself in a few words:</TD>

</TR><TR>

<TD COLSPAN=”2”><TEXTAREA NAME=”biog” ROWS=”4” COLS=”47”></TEXTAREA></TD>

</TR><TR>

<TD COLSPAN=”2”><INPUT TYPE=”SUBMIT” VALUE=”Send” /></TD>

</TR>

</FORM>

</TABLE>

<?

}

?>

</BODY>

</HTML>



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.